package com.linyaonan.leetcode.medium._215;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author: Lin
 * @date: 2024/9/29
 */
public class KthLargestElementInAnArray {

    /**
     * 优先级队列
     * @param nums
     * @param k
     * @return
     */
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.comparingInt(o -> -o));
        for (int num : nums) {
            queue.add(num);
        }

        for (int i = 0; i < k - 1; i++) {
            queue.poll();
        }
        return queue.peek();
    }

}
